package com.dai.code;

import com.dai.common.Node;

/**
 * 反转链表
 */
public class ReverseLinkedList1 {

    public static void main(String[] args) {
        Node head = new Node(1);
        Node temp = head;
        for (int i = 2; i < 10; i++) {
            temp.next = new Node(i);
            temp = temp.next;
        }
        System.out.println(head + "反转" + reverseLinkedList(head));
    }


    public static Node reverseLinkedList(Node head) {
        if (null == head || null == head.next) {
            return head;
        }
        Node pre = head;
        Node cur = head.next;
        Node next = cur;
        pre.next = null;

        while (next != null) {
            next = next.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }

        return pre;
    }
}
